﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewLandOY20
{
    public partial class AddUserForm : Form
    {
        public AddUserForm()
        {
            InitializeComponent();
        }
        DataTable tableUser = null;
        // 从InboxFrom接收AccessHelper对象
        private AccessHelper accessHelper;
        public AccessHelper Helper
        {
            set
            {
                accessHelper = value;
            }
        }

        /// <summary>
        /// 窗体load事件，将用户信息显示出来
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddUserForm_Load(object sender, EventArgs e)
        {
            if(accessHelper != null)
            {
                refershDataGridView();
            }
        }

        /// <summary>
        /// 刷新dataGridView的数据显示
        /// </summary>
        private void refershDataGridView()
        {
            tableUser = accessHelper.ExecuteDataTable(MyAccessSQLStatement.SelectAllUser, null);
            dataGridView_User.DataSource = tableUser;
        }

        /// <summary>
        /// 添加按钮的点击事件
        /// 判断用户输入的数据是否为空，是否和正则表达式匹配
        /// 确认信息无误，将数据写入数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_addUser_Click(object sender, EventArgs e)
        {
            if(textBox_UserID.Text != "" && textBox_UserName.Text != "" && textBox_UserAddress.Text != "" 
                && textBox_Phone.Text != "" && textBox_Company.Text != "" && Regex.IsMatch(textBox_UserID.Text, MyPatternStr.RegexUser)
                && Regex.IsMatch(textBox_Phone.Text, MyPatternStr.RegexPhone))
            {
                StringBuilder sqlStr = new StringBuilder("INSERT INTO [User] VALUES ('");
                sqlStr.Append(textBox_UserID.Text);
                sqlStr.Append("','");
                sqlStr.Append(textBox_UserName.Text);
                sqlStr.Append("','");
                sqlStr.Append(textBox_UserAddress.Text);
                sqlStr.Append("','");
                sqlStr.Append(textBox_Phone.Text);
                sqlStr.Append("','");
                sqlStr.Append(textBox_Company.Text);
                sqlStr.Append("');");

                //MessageBox.Show(sqlStr.ToString());
                int changeCount = accessHelper.ExecuteNonQuery(sqlStr.ToString(), null);
                if(changeCount > 0)
                {
                    //MessageBox.Show("添加成功！", "提示");
                    refershDataGridView();
                }
            }
            else
            {
                MessageBox.Show("请检查用户编号或者手机号码格式是否正确，或者是否有信息未录入！", "提示");
            }
        }

        private void textBox_Phone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
                {
                    e.Handled = true;
                }
            }
        }

        /// <summary>
        /// 修改用户的信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_updateUser_Click(object sender, EventArgs e)
        {
            if(textBox_UserID.Text != "" && textBox_UserName.Text != "" && textBox_UserAddress.Text != "" 
                && textBox_Phone.Text != "" && textBox_Company.Text != "" && Regex.IsMatch(textBox_UserID.Text, MyPatternStr.RegexUser)
                && Regex.IsMatch(textBox_Phone.Text, MyPatternStr.RegexPhone))
            {
                StringBuilder sqlStr = new StringBuilder("UPDATE [User] SET 收货人姓名='");
                sqlStr.Append(textBox_UserName.Text);
                sqlStr.Append("', 收货地址='");
                sqlStr.Append(textBox_UserAddress.Text);
                sqlStr.Append("', 联系电话='");
                sqlStr.Append(textBox_Phone.Text);
                sqlStr.Append("', 公司='");
                sqlStr.Append(textBox_Company.Text);
                sqlStr.Append("' WHERE 用户号='");
                sqlStr.Append(textBox_UserID.Text);
                sqlStr.Append("';");

                //MessageBox.Show(sqlStr.ToString());
                accessHelper.ExecuteNonQuery(sqlStr.ToString(), null);
                refershDataGridView();
            }
            else
            {
                MessageBox.Show("请检查用户编号或者手机号码格式是否正确，或者是否有信息未录入！", "提示");
            }
        }

        /// <summary>
        /// 双击用户号，将这条记录显示在输入框内进行修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView_User_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            textBox_UserID.Text = tableUser.Rows[rowIndex]["用户号"].ToString();
            textBox_UserName.Text = tableUser.Rows[rowIndex]["收货人姓名"].ToString();
            textBox_UserAddress.Text = tableUser.Rows[rowIndex]["收货地址"].ToString();
            textBox_Phone.Text = tableUser.Rows[rowIndex]["联系电话"].ToString();
            textBox_Company.Text = tableUser.Rows[rowIndex]["公司"].ToString();
        }

        /// <summary>
        /// 删除指定用户编号的用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_deleteUser_Click(object sender, EventArgs e)
        {
            string idStr = textBox_UserID.Text;
            if(idStr != "" && Regex.IsMatch(idStr, MyPatternStr.RegexUser))
            {
                if(MessageBox.Show("是否要删除用户编号为："+idStr+"的用户？","提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    StringBuilder sqlStr = new StringBuilder("DELETE FROM [User] WHERE 用户号='");
                    sqlStr.Append(idStr);
                    sqlStr.Append("';");
                    accessHelper.ExecuteNonQuery(sqlStr.ToString(), null);
                    refershDataGridView();
                }
            }
        }

    }
}
